package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author mengwei
 * @date 2019/8/19
 * @desc
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void addMember(Member member) {
        memberDao.addMember(member);
    }

    /**
     * 折线图动态查询
     * @param days
     * @return
     */
    @Override
    public List <Long> findMemberByDate(List <String> days) {
        List<Long> list =new ArrayList <>();
        for (String date : days) {
            String beginDate =date+"-1";
            String endDate =date+"-31";
            Long count = memberDao.findMemberByDate(beginDate,endDate);
            list.add(count);
        }
        return list;
    }




    @Override
    public Map<String, Object> getMemberReport() {
        List<String> months = new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        //获取前12个月份
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String date = simpleDateFormat.format(calendar.getTime());
            months.add(date);
        }
        //统计每个月对应的会员数量
        for (String month : months) {
            month += "-31";
            long count = memberDao.findCountBeforeByDate(month);
            memberCount.add(count);
        }

        Map<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);
        return map;
    }


    /*
    * 会员统计饼图
    * 按照性别分类
    * */
    @Override
    public Map<String, Object> getMemberSex() {
        //获取memberCount数据
        List<Map<String,String>> mapList=memberDao.getMemberSex();
        //取出mapList集合中的name
        List<String> memberSex=new ArrayList<>();
        //遍历集合获取name对象
        for (Map<String, String> map : mapList) {
            String sex = map.get("name");
            //将数据库中的数字转换
            if ("1".equals(sex)){
                map.put("name","男");
                sex="男";
                memberSex.add(sex);
            }else if ("2".equals(sex)){
                map.put("name","女");
                sex="女";
                memberSex.add(sex);
            }else {
                map.put("name","未填写");
                sex="未填写";
                memberSex.add(sex);
            }
        }
        //组合数据
        Map<String,Object> resultMap= new HashMap<>();
        resultMap.put("memberSex",memberSex);
        resultMap.put("memberCount",mapList);
        return resultMap;
    }
    /*
     *会员统计饼图
     * 按照年龄段分类
     * */
    @Override
    public Map<String, Object> getMemberAge() {
        //获取count数据
        List<Map<String,Object>> memberCount=new ArrayList<>();
        //查询身份证号
        List<Member> memberList=memberDao.getAllidCate();
        List<String> nameList=new ArrayList<>();
        //为每个年龄段创建集合
        Map<String,Object> memberMap1=new HashMap<>();
        Map<String,Object> memberMap2=new HashMap<>();
        Map<String,Object> memberMap3=new HashMap<>();
        Map<String,Object> memberMap4=new HashMap<>();
        //创建当前日期
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
        Integer nowYear =Integer.valueOf(simpleDateFormat.format(new Date()));
        int num1=0;
        int num2=0;
        int num3=0;
        int num4=0;
        for (Member member : memberList) {
            //获取身份证号码
            String idCard = (String) member.getIdCard();
            //截取到年份日期
            Integer birthday = Integer.valueOf(idCard.substring(6, 10));
            //根据年份日期计算年龄
            Integer Age=nowYear-birthday;
//            System.out.println(Age);
            //判断年龄段
            if (Age>0&&Age<=18){
                memberMap1.put("name","0-18");
                memberMap1.put("value",++num1);
            }else if (Age>18&&Age<=30){
                memberMap2.put("name","18-30");
                memberMap2.put("value",++num2);
            }else if (Age>30&&Age<=45){
                memberMap3.put("name","30-45");
                memberMap3.put("value",++num3);
            }else if (Age>45){
                memberMap4.put("name","45岁以上");
                memberMap4.put("value",++num4);
            }

        }
        //将年龄段存储到集合中
        memberCount.add(memberMap1);
        memberCount.add(memberMap2);
        memberCount.add(memberMap3);
        memberCount.add(memberMap4);
        //遍历集合添加name
        for (Map<String, Object> map : memberCount) {
            String name = (String) map.get("name");
            nameList.add(name);
        }
        //整合数据
        Map<String,Object> resultMap= new HashMap<>();

        resultMap.put("memberAge",nameList);
        resultMap.put("memberCount",memberCount);
//        System.out.println(resultMap);
        return resultMap;
    }

}
